﻿using System;
using System.Linq;
using System.Data;
using System.Dynamic;
using System.Security.Cryptography;
using System.Text;
using System.Reflection;
using Nancy;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Dapper;
using API_Service.DBHelper;

namespace API_Service.BizLogic.Platform
{
    public class PlatformService : BaseLogicService
    {
        public PlatformService() { }
        public ExecutionResult UpdatePassowrd(string userid, string password)
        {
            var result = new ExecutionResult();

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    conn.Open();

                    string sql = "update c_emp_desc_t set password=@password where userid=@userid ";

                    result.ExecuteCount = conn.Execute(sql, new { Password = password, UserID = userid });

                    result.Status = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }
        public ExecutionResult<UserDentity> Login(UserDentity dentity)
        {
            var result = new ExecutionResult<UserDentity>();

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    conn.Open();

                    string sql = "select userid, username, password from c_emp_desc_t where userid=@userid collate Chinese_PRC_CS_AI_WS and password=@password collate Chinese_PRC_CS_AI_WS";

                    result.Datas = conn.Query<UserDentity>(sql, dentity).ToArray();

                    if (result.Datas.Length < 1)
                    {
                        result.Status = false;
                        result.Message = "用户名或者密码错误.";
                    }
                    else
                    {
                        result.Status = true;
                    }

                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }
        public ExecutionResult<dynamic> GetPDAMenuList(string userid)
        {
            var result = new ExecutionResult<dynamic>();

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    conn.Open();

                    string sql = " select distinct d.* from c_emp_desc_t a, c_emp_role_t b, c_role_menu_t c, c_menu_desc_t d " +
                                 " where a.userid = b.userid and b.role_id = c.role_id and c.menu_id = d.menu_id and a.userid = @userid and d.resource_type = 'PDA' " +
                                 " order by d.parent_id, menu_seq; ";

                    result.Datas = conn.Query<dynamic>(sql, new { userid = userid }).ToArray();

                    result.Status = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }
        public ExecutionResult<dynamic> GetWINMenuList(string userid)
        {
            var result = new ExecutionResult<dynamic>();

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    conn.Open();

                    string sql = " select distinct d.* from c_emp_desc_t a, c_emp_role_t b, c_role_menu_t c, c_menu_desc_t d " +
                                 " where a.userid = b.userid and b.role_id = c.role_id and c.menu_id = d.menu_id and a.userid = @userid and d.resource_type = 'WIN' " +
                                 " order by d.parent_id, menu_seq; ";

                    result.Datas = conn.Query<dynamic>(sql, new { userid = userid }).ToArray();

                    result.Status = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }
        public ExecutionResult<dynamic> GetWEBMenuList(string userid)
        {
            var result = new ExecutionResult<dynamic>();

            using (IDbConnection conn = SqlCreator.Create())
            {
                try
                {
                    conn.Open();

                    string sql = " select distinct d.* from c_emp_desc_t a, c_emp_role_t b, c_role_menu_t c, c_menu_desc_t d " +
                                 " where a.userid = b.userid and b.role_id = c.role_id and c.menu_id = d.menu_id and a.userid = @userid and d.resource_type = 'WEB' " +
                                 " order by d.parent_id, menu_seq; ";

                    result.Datas = conn.Query<dynamic>(sql, new { userid = userid }).ToArray();

                    result.Status = true;
                }
                catch (Exception ex)
                {
                    Logger.WriteError(MethodBase.GetCurrentMethod(), ex);
                    result.Status = false;
                    result.Message = "Error: " + ex.Message;
                }
                finally
                {
                    conn.Close();
                }
            }

            return result;
        }
    }
}